package com.wnxy.consumer.f_confirm;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 死信消费者
 */
@Component
public class DLQConsumer {

    /**
     * 死信队列：    queue-dlx
     * 死信交换机： exchange-dlx
     * 路由key：     dlx.#
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "queue-dlx",durable = "true"),
            exchange = @Exchange(name = "exchange-dlx",type = ExchangeTypes.TOPIC),
            key = {"dlx.#"}
    ))
    public void handlerMsg(@Payload OrderDto orderDto, Message message, Channel channel) throws IOException {
        System.out.println("死信消费者：" + orderDto);
        // 消息消费手动确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
